<template>
  <div class="select-users">
    <span style="display: none">{{ childDataSource }}</span>
    <a-table
      :data-source="childDataSource"
      bordered
      :pagination="false"
      size="small"
    >
      <a-table-column key="No" title="No" align="center">
        <template slot-scope="text, record, ind">
          {{ ind + 1 }}
        </template>
      </a-table-column>
      <a-table-column
        key="SupplementDetailName"
        :title="$t('breach.steps.step2.business.form.where.inside.system')"
        align="center"
        v-if="ifSystem"
      >
        <template slot-scope="text, record">
          <a-input
            v-model="record.SupplementDetailName"
            :disabled="disabled"
            :placeholder="$t('breach.steps.step1.content.form.tip')"
          ></a-input>
        </template>
      </a-table-column>
      <a-table-column
        key="SupplementDetailContacts"
        :title="$t('breach.steps.step2.business.form.call.user')"
        align="center"
        width="240px"
      >
        <template slot-scope="text, record">
          <a-select
            :placeholder="$t('breach.steps.step1.content.form.tip1')"
            v-model="record.SupplementDetailContacts"
            show-search
            :disabled="disabled"
            @search="getUsers"
            @change="(...rest) => getDetails(record.Id, ...rest)"
          >
            <a-select-option
              v-for="item in userList"
              :value="item.value"
              :key="item.value"
            >
              {{ item.label }}
            </a-select-option>
          </a-select>
        </template>
      </a-table-column>
      <a-table-column
        key="SupplementDetailDepartment"
        :title="$t('breach.steps.step2.business.form.call.department')"
        align="center"
      >
        <template slot-scope="text, record">
          {{ record.SupplementDetailDepartment }}
        </template>
      </a-table-column>
      <a-table-column
        key="SupplementDetailEmail"
        :title="$t('breach.steps.step2.business.form.call.email')"
        align="center"
      >
        <template slot-scope="text, record">
          {{ record.SupplementDetailEmail }}
        </template>
      </a-table-column>
      <a-table-column
        key="SupplementDetailRemark"
        :title="$t('breach.steps.step2.business.form.call.remark')"
        align="center"
      >
        <template slot-scope="text, record">
          <a-input
            v-model="record.SupplementDetailRemark"
            :disabled="disabled"
            :placeholder="$t('breach.steps.step1.content.form.tip')"
          ></a-input>
        </template>
      </a-table-column>
      <a-table-column key="operate" :title="$t('di.menus')" align="center">
        <template slot-scope="text, record">
          <a-button
            type="link"
            :disabled="disabled"
            @click="delRow(record.Id)"
            >{{ $t("app.setting.copy") }}</a-button
          >
        </template>
      </a-table-column>
    </a-table>
    <a-button
      icon="plus"
      :disabled="disabled"
      @click="childDataSource.push({ Id: +new Date() })"
      >{{ $t("breach.steps.step1.content.form.table.add") }}</a-button
    >
  </div>
</template>

<script>
import { getUserList } from "@/api/common/form";
export default {
  name: "selectUserList",
  props: ["dataSource", "ifSystem", "disabled", "initUserList"],
  data() {
    return {
      childDataSource: [],
      userList: [],
    };
  },
  watch: {
    dataSource: {
      handler(newVal) {
        this.childDataSource = newVal || [];
      },
      deep: true,
      immediate: true,
    },
    childDataSource: {
      handler(newVal) {
        this.$emit("updateData", newVal);
      },
      deep: true,
      immediate: true,
    },
    initUserList: {
      handler() {
        this.getUsers();
      },
      immediate: true,
      deep: true,
    },
  },
  methods: {
    delRow(Id) {
      this.childDataSource = this.childDataSource.filter(
        (item) => item.Id !== Id
      );
    },
    async getUsers(value) {
      const { data } = await getUserList({ UserEmail: value, UserName: value });
      this.userList = this.initUserList
        ? [...this.initUserList, ...data]
        : data;
    },
    // 获取对应 部门/邮箱
    getDetails(Id, value) {
      let currentInd = -1;
      this.childDataSource.forEach((item, ind) => {
        if (item.Id === Id) currentInd = ind;
      });
      this.childDataSource[currentInd].SupplementDetailDepartment =
        value.split("/")[1];
      this.childDataSource[currentInd].SupplementDetailEmail =
        value.split("/")[2];
    },
  },
};
</script>

<style>
</style>